import ISettingsController
  from '../../../../../../../common/component/src/main/ets/default/controller/ISettingsController';
import SwitchController from '../../../../../../../common/component/src/main/ets/default/controller/SwitchController';
import systemParameterEnhance from '@ohos.systemParameterEnhance';
import { ConfigData, LogUtil, UpdateUtil } from '@common/utils';

const MODULE_TAG = ConfigData.TAG + '.UpdateController -> ';

export default class UpdateController extends SwitchController {
  initData(): ISettingsController {
    super.initData();
    return this;
  }

  /**
   * Get usb current functions state
   */
  getUsbCurrentFunctions() {
  }

  /**
   * After current value changed event
   */
  afterCurrentValueChanged() {
    if (this.isOn) {
      try {
        systemParameterEnhance.setSync("persist.settings.update", "true")
        LogUtil.info(MODULE_TAG + "enable update success ")
        UpdateUtil.checkUpdate().then(val => {
          AppStorage.setOrCreate('updateAvail', val);
        })
      } catch (error) {
        LogUtil.info(MODULE_TAG + "enable update fail " + JSON.stringify(error))
      }
    } else {
      try {
        systemParameterEnhance.setSync("persist.settings.update", "false")
        AppStorage.setOrCreate('updateAvail', false);
        LogUtil.info(MODULE_TAG + "disable update success ")
      } catch (error) {
        LogUtil.info(MODULE_TAG + "disable update fail " + JSON.stringify(error))
      }
    }
  }
}